<?php
// Zend upload by Rika ver 1.0
class Private_Upload extends Zend_File_Transfer_Adapter_Http{
	public function upload($dir){
		//$adapter = new Zend_File_Transfer_Adapter_Http();
		$this->setDestination(ROOT_DIR."/homeviet/public/uploaded/images/".$dir);
	
		$upload = new Zend_File_Transfer();
		$upload->addValidator('Extension', false, 'jpg,png,gif,jpeg');
		$files = $upload->getFileInfo();
		//print_r($files);
		foreach ($files as $file => $info) {
			 
			// validators are ok ?
			if (!$upload->isValid($file)) {
				echo "File tải lên không hợp lệ !!!";
				continue;
			}else {
				$this->receive();
				//echo "file Ä‘Ã£ Ä‘Æ°á»£c gá»­i lÃªn";
			}
		}
			 
		//@unlink($image);//hÃ m xÃ³a file cÅ© nhÆ°ng chÆ°a viáº¿t tá»›i
	}
	

	## Ä‘Ã¢y lÃ  hÃ m upload hÃ¬nh áº£nh----------------------------
	
	function uploadImage($inputName, $uploadDir, $uploadDirThumb)
	{
		$image = $_FILES[$inputName];
		$imagePath = '';
		if (trim($image['tmp_name']) != '')
		{
			$ext = substr(strrchr($image['name'], "."), 1);
			$imagePath=md5(rand() * time()).".$ext";
			$extension=$this->get_image_extension($imagePath);
			if(($extension != "jpg") && ($extension != "jpeg") &&
					($extension != "png") && ($extension != "gif") )
			{
				exit();
			} else {
				$move=@copy($image['tmp_name'], $uploadDir . $imagePath);
				if($move){
					$this->create_thumbnail($uploadDir.$imagePath,$uploadDirThumb.$imagePath,140,155);
				} else {
					exit();
				}
			}
		}
		return $imagePath;
	}
	
	## Ä‘Ã¢y lÃ  hÃ m táº¡o imagethumbnail vÃ  random hÃ¬nh áº£nh--------------------
	function create_thumbnail($source, $destination, $thumb_width, $thumb_height)	{
	$size=getimagesize($source);
	$width=$size[0];
	$height=$size[1];
	$x=0;	$y=0;
	
	$new_image=imagecreatetruecolor($thumb_width,$thumb_height) or die();
	$extension=$this->get_image_extension($source);
	if($extension=="jpg" || $extension=="jpeg")
		$image=imagecreatefromjpeg($source);
		if($extension=="gif")
		$image=imagecreatefromgif($source);
		if($extension=="png")
		$image=imagecreatefrompng($source);
		imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_height,$width,$height);
		if($extension=="jpg" || $extension=="jpeg")
			imagejpeg($new_image,$destination);
			if($extension=="gif")
				imagegif($new_image,$destination);
				if($extension=="png")
				imagepng($new_image,$destination);
	}
	function get_image_extension($name)	{
	$name=strtolower($name);
	$i=strrpos($name,".");
	if(!$i){	return "";	}
	$l=strlen($name)-$i;
	$extension=substr($name,$i+1,$i);
	return $extension;
	}
	
	public function del_img($patch){
		@unlink(ROOT_DIR."/homeviet".$patch);
		@unlink(ROOT_DIR."/homeviet".$patch);
	}
	
}#end class ---------------
	/*
	* cÃ¡ch sá»­ dá»¥ng nhÆ° sau -----------
	khi táº¡o hÃ m insert hoáº·c update báº¡n chá»‰ cáº§n thÃªm Ä‘oáº¡n nÃ y vÃ o nhÆ° sau
	$tÃªn_biáº¿n =$_POST['inputName']; thÃ¬ ta sáº½ Ä‘á»•i láº¡i lÃ  hÃ m dÆ°á»›i Ä‘Ã¢y.
	
	$imagePro = $this->uploadImage('image','Ä‘Æ°á»�ng dáº«n cá»§a hÃ¬nh','Ä‘Æ°á»�ng dáº«n táº¡o hÃ¬nh thumbnail');
	*/